package com.wechat.mp.handler;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;

/**
 * @author hnyufushan
 */
@Slf4j
@Component
public class MenuHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        String msg = String.format("type:%s, event:%s, key:%s",
            wxMessage.getMsgType(), wxMessage.getEvent(),
            wxMessage.getEventKey());
        if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
            return null;
        }
        if(MenuButtonType.CLICK.equalsIgnoreCase(wxMessage.getEvent())){
            // 图文消息入口
            if("V1001_ARTICLES".equalsIgnoreCase(wxMessage.getEventKey())){
                WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
                item.setTitle("图文消息测试");
                item.setDescription("图文消息信息，注意，如果图文数超过限制，则将只发限制内的条数");
                item.setPicUrl("http://img1.imgtn.bdimg.com/it/u=2882141390,2731945707&fm=11&gp=0.jpg");
                item.setUrl("http://www.chinazhaolong.com/aierlan/lxzx/zl38186.html");
                return WxMpXmlOutMessage.NEWS().addArticle(item).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
                    .build();
            }
            if("V1001_GOOD".equalsIgnoreCase(wxMessage.getEventKey())){
                System.out.println(".......");
                return null;
            }
        }

        return WxMpXmlOutMessage.TEXT().content(msg)
            .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
            .build();
    }

}
